package com.csnf.servlet;

import com.csnf.dao.IClazzDao;
import com.csnf.dao.IStudentDao;
import com.csnf.dao.impl.ClazzDaoImpl;
import com.csnf.dao.impl.StudentDaoImpl;
import com.csnf.entity.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDate;

@WebServlet({"/StudentServlet","/deleteById","/getStudentById","/addStudent"})
public class StudentServlet  extends HttpServlet {
    IStudentDao studentDao = new StudentDaoImpl();
    IClazzDao clazzDao = new ClazzDaoImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取servlet路径
        String servletPath = req.getServletPath();
        switch (servletPath){
            //根据id查询学生信息
            case "/getStudentById":
                req.setAttribute("stu", studentDao.findById(Integer.parseInt(req.getParameter("sId"))));
                req.setAttribute("clazzList", clazzDao.findAll());
                req.getRequestDispatcher("editStudent.jsp").forward(req, resp);
                break;
            //根据id删除学生信息
            case "/deleteById":
                studentDao.deleteById(Integer.parseInt(req.getParameter("sId")));
                req.setAttribute("stuList", studentDao.findAll());
                break;
            // 查询学生信息
            case "/StudentServlet":
                String sName = req.getParameter("sName");
                //判断是否为空 如果是空则查询所有，否则根据姓名查询
                if(sName!=null){
                    req.setAttribute("stuList", studentDao.findByName(sName));
                }else{
                    req.setAttribute("stuList", studentDao.findAll());
                }
                break;
        }
        req.getRequestDispatcher("StudentList.jsp").forward(req, resp);
    }




    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        //获取表单数据
        String sName = req.getParameter("sName");
        String birth = req.getParameter("birth");
        String gender = req.getParameter("gender");
        String clazzId = req.getParameter("cId");
        //获取servlet路径判断调用方法
        String servletPath = req.getServletPath();
        switch (servletPath){
            // 添加学生信息
            case "/addStudent":
                studentDao.insert(new Student(null, sName, LocalDate.parse(birth), gender,Integer.parseInt(clazzId)));
                break;
            // 修改学生信息
            case "/StudentServlet":
                String sId = req.getParameter("sId");
                studentDao.update(new Student(Integer.parseInt(sId), sName, LocalDate.parse(birth), gender, Integer.parseInt(clazzId)));
                break;
        }
        //重新查询
        req.setAttribute("stuList", studentDao.findAll());
        //转发学生列表页面
        req.getRequestDispatcher("StudentList.jsp").forward(req, resp);
    }
}
